package convertor

import (
	"algo-sdk-go/common/tools"
	"algo-sdk-go/project/matchmaking/adapter/po"
	"algo-sdk-go/project/matchmaking/common/biz_tools"
	"algo-sdk-go/project/matchmaking/common/constant"
	"algo-sdk-go/project/matchmaking/domain/models/entity"

	"go.mongodb.org/mongo-driver/bson/primitive"
)

func ConvertUserProfileToPo(userProfile *entity.UserProfile) *po.UserProfile {
	age, zodiac, err := biz_tools.CalculateAgeAndZodiac(userProfile.BirthDate)
	if err != nil {
		return nil
	}

	return &po.UserProfile{
		Id:             primitive.NewObjectID().Hex(),
		UserNum:        userProfile.UserNum,
		UserAvatar:     userProfile.UserAvatar,
		Name:           userProfile.Name,
		Gender:         userProfile.Gender.Value(),
		BirthDate:      userProfile.BirthDate,
		Age:            age,
		ChineseZodiac:  zodiac,
		Height:         userProfile.Height,
		Weight:         userProfile.Weight,
		BMIDesc:        biz_tools.GetBMIDesc(userProfile.Height, userProfile.Weight),
		MaritalStatus:  userProfile.MaritalStatus,
		Education:      userProfile.Education,
		EducationLevel: userProfile.Education.Level(),
		Assets: constant.AssetInfo{
			AnnualIncome: userProfile.Assets.AnnualIncome,
			Car:          userProfile.Assets.Car,
			House:        userProfile.Assets.House,
		},
		IsPartyMember:    userProfile.IsPartyMember,
		Occupation:       userProfile.Occupation,
		Hometown:         userProfile.Hometown,
		CurrentResidence: userProfile.CurrentResidence,
		FamilyInfo: constant.FamilyInfo{
			FamilyType: userProfile.FamilyInfo.FamilyType,
			FatherDetail: constant.RelationDetail{
				Age:  userProfile.FamilyInfo.FatherDetail.Age,
				Desc: userProfile.FamilyInfo.FatherDetail.Desc,
			},
			MotherDetail: constant.RelationDetail{
				Age:  userProfile.FamilyInfo.MotherDetail.Age,
				Desc: userProfile.FamilyInfo.MotherDetail.Desc,
			},
			OtherDetail: userProfile.FamilyInfo.OtherDetail,
		},
		Verified:   userProfile.Verified,
		Status:     userProfile.Status,
		IsDeleted:  false,
		CreateUser: userProfile.CreateUser,
		CreateTime: tools.TimeNowStr(),
		ModifyUser: userProfile.ModifyUser,
		UpdateTime: tools.TimeNowStr(),
	}
}

func ConvertUserProfileToEntity(userProfile *po.UserProfile) entity.UserProfile {
	return entity.UserProfile{
		UserNum:        userProfile.UserNum,
		UserAvatar:     userProfile.UserAvatar,
		Name:           userProfile.Name,
		Gender:         constant.Gender(userProfile.Gender),
		BirthDate:      userProfile.BirthDate,
		Age:            userProfile.Age,
		ChineseZodiac:  userProfile.ChineseZodiac,
		Height:         userProfile.Height,
		Weight:         userProfile.Weight,
		BMIDesc:        tools.TernaryExpression(userProfile.BMIDesc != "", userProfile.BMIDesc, biz_tools.GetBMIDesc(userProfile.Height, userProfile.Weight)),
		MaritalStatus:  userProfile.MaritalStatus,
		Education:      userProfile.Education,
		EducationLevel: userProfile.Education.Level(),
		Assets: constant.AssetInfo{
			AnnualIncome: userProfile.Assets.AnnualIncome,
			Car:          userProfile.Assets.Car,
			House:        userProfile.Assets.House,
		},
		IsPartyMember:    userProfile.IsPartyMember,
		Occupation:       userProfile.Occupation,
		Hometown:         userProfile.Hometown,
		CurrentResidence: userProfile.CurrentResidence,
		FamilyInfo: constant.FamilyInfo{
			FamilyType: userProfile.FamilyInfo.FamilyType,
			FatherDetail: constant.RelationDetail{
				Age:  userProfile.FamilyInfo.FatherDetail.Age,
				Desc: userProfile.FamilyInfo.FatherDetail.Desc,
			},
			MotherDetail: constant.RelationDetail{
				Age:  userProfile.FamilyInfo.MotherDetail.Age,
				Desc: userProfile.FamilyInfo.MotherDetail.Desc,
			},
			OtherDetail: userProfile.FamilyInfo.OtherDetail,
		},
		Status:     userProfile.Status,
		Verified:   userProfile.Verified,
		CreateUser: userProfile.CreateUser,
		CreateTime: userProfile.CreateTime,
		ModifyUser: userProfile.ModifyUser,
		UpdateTime: userProfile.UpdateTime,
	}
}
